package com.cooper.mediadb.online;

import java.io.IOException;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.cooper.scraper.DownloadException;
import com.cooper.scraper.HttpDownload;
import com.cooper.scraper.ScraperException;

public class AnalyzeSHOUHU extends AnalyzeVideo {

	public AnalyzeSHOUHU(HttpDownload httpDownload, String url, String format) {
		super(httpDownload, url, format);
	}

	@Override
	public List<String> analyzeVideo() throws DownloadException,
			ScraperException, IOException {
		return analyze(url);
	}

	private final static String VEDIO_INFO_BASE_URL = "http://hot.vrs.sohu.com/vrs_flash.action?vid=";

	private List<String> analyze(String analyzeUrl) throws DownloadException,
			MalformedURLException, IOException {

		if (Thread.currentThread().isInterrupted()) {
			return new ArrayList<String>(0);
		}
		String page = httpDownload.downloadPage(analyzeUrl);
		if (Thread.currentThread().isInterrupted()) {
			return new ArrayList<String>(0);
		}
		Pattern p = Pattern.compile("var vid=\"(.+?)\";");
		Matcher m = p.matcher(page);
		if (!m.find()) {
			Pattern p2 = Pattern
					.compile("<a href=\"(http://[^/]+/[0-9]+/[^\\.]+\\.shtml)\" target=\"?_blank\"?><img");
			Matcher m2 = p2.matcher(page);
			if (m2.find()) {
				return analyze(m2.group(1));
			} else {
				return new ArrayList<String>();
			}
		} else {
			String videoId = m.group(1);
			if (videoId.contains(",")) {
				videoId = videoId.split(",")[0];
			}
			page = httpDownload.downloadPage(VEDIO_INFO_BASE_URL + videoId);
			if (Thread.currentThread().isInterrupted()) {
				return new ArrayList<String>(0);
			}
			p = Pattern
					.compile("\"norVid\":(.+?),\"highVid\":(.+?),\"superVid\":(.+?),");
			m = p.matcher(page);
			Map<String, String> formats = new HashMap<String, String>();
			if (m.find()) {
				if (!"0".equals(m.group(1))) {
					formats.put(MediaSrcMgr.FORMAT_NORMAL, m.group(1));
				}
				if (!"0".equals(m.group(2))) {
					formats.put(MediaSrcMgr.FORMAT_HIGH, m.group(2));
				}
				if (!"0".equals(m.group(3))) {
					formats.put(MediaSrcMgr.FORMAT_SUPER, m.group(3));
				}
			}
			if (!formats.isEmpty() && !Thread.currentThread().isInterrupted()) {
				String selectFormatVedio = getSelectFormatVideoId(formats);
				if (selectFormatVedio != null
						&& !selectFormatVedio.equals(videoId)
						&& !Thread.currentThread().isInterrupted()) {
					page = httpDownload.downloadPage(VEDIO_INFO_BASE_URL
							+ selectFormatVedio);
				}
				if (Thread.currentThread().isInterrupted()) {
					return new ArrayList<String>(0);
				}

				String[] paths;
				p = Pattern.compile("\"clipsURL\"\\:\\[\"(.+?)\"\\]");
				m = p.matcher(page);
				if (m.find()) {
					paths = m.group(1).split("\",\"");
				} else
					return new ArrayList<String>(0);

				String[] newPatchs;
				p = Pattern.compile("\"su\"\\:\\[\"(.+?)\"\\]");
				m = p.matcher(page);
				if (m.find()) {
					newPatchs = m.group(1).split("\",\"");
				} else
					return new ArrayList<String>(0);

				List<String> result = new ArrayList<String>();
				for (int i = 0; i < paths.length; i++) {
					String[] vedioInfos = httpDownload.downloadPage(
							"http://data.vod.itc.cn/?prot=2&file="
									+ paths[i].replace(
											"http://data.vod.itc.cn", "")
									+ "&new=" + newPatchs[i]).split("\\|");

					if (Thread.currentThread().isInterrupted()) {
						return new ArrayList<String>(0);
					}
					// 备选服务器
					// httpConn = httplib.HTTPConnection("new.sohuv.dnion.com")
					// httpConn.request('GET', newpaths[i]+'?key='+key)

					result.add(httpDownload.getRedirectUrl(vedioInfos[0]
							.substring(0, vedioInfos[0].length() - 1)
							+ newPatchs[i] + "?key=" + vedioInfos[3]));
					// url=response.getheader('Location'); //得到文件位置
				}
				return result;
			}

		}
		return new ArrayList<String>(0);
	}
}
